//
//  MYGAddCaseFilesTimeCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/4/9.
//

import UIKit
import BRPickerView


class MYGAddCaseFilesTimeCell: UITableViewCell {

    var inputDidChange:((String,Int) -> ())?
    var index:Int = 0
    var data:[String:String]? {
        didSet {
            guard let model = data else {
                return
            }
            titleLabel.text = model["name"]
            textFiled.text = model["value"]
            if model["name"] == "就诊日期"{
                saveButton.isHidden = false
                textFiled.snp.remakeConstraints { make in
                    make.right.equalTo(saveButton.snp.left).offset(-5)
                    make.centerY.equalToSuperview()
                }
            }else {
                saveButton.isHidden = true
                textFiled.snp.remakeConstraints { make in
                    make.right.equalToSuperview().offset(-20)
                    make.centerY.equalToSuperview()
                }
            }
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        contentView.backgroundColor = .rgba(255, 255, 255, 1)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(20)
        }
        contentView.addSubview(saveButton)
        saveButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
            make.right.equalToSuperview().offset(-20)
        }
        contentView.addSubview(textFiled)
        textFiled.snp.makeConstraints { make in
            make.right.equalTo(saveButton.snp.left).offset(-5)
            make.centerY.equalToSuperview()
        }
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(23)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        saveButton.isHidden = true
    }
    
    private lazy var titleLabel:UILabel = {
       let cont = UILabel()
        cont.font = .regular(14)
        cont.textColor = .rgba(80, 80, 80, 1)
        cont.text = "名称"
        return cont
    }()
    
    private lazy var saveButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Health/Stastisc/date"), for: .normal)
        btn.addTarget(self, action: #selector(selectDate), for: .touchUpInside)
        return btn
    }()
    
    var textFiled: UITextField = {
       let tf = UITextField()
       tf.textAlignment = .right
        //tf.delegate = self
       tf.textColor = .rgba(51, 51, 51)
       tf.font = .regular(16)
       tf.placeholder = "请输入 "
        tf.addTarget(self, action: #selector(textFiledEditingDidChange), for: .editingChanged)
       return tf
   }()
    
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .rgba(238, 238, 238, 1)
        return line
    }()
    
    @objc func textFiledEditingDidChange() {
        inputDidChange?(textFiled.text ?? "",index)
    }
    
    
    @objc
    func selectDate() {
        
        BRDatePickerView.showDatePicker(with: .YMDHMS, title: "", selectValue: "") { [self] _, date in
            textFiled.text = date
            inputDidChange?(date!,index)
        }
    }
}
